# -*-coding:utf-8-*-
import sys
from YX_crawler.props.sql_connect import Base, DBSession
import sqlalchemy
from sqlalchemy import Column, String, Float, Date, func
import random
import threading
from YX_crawler.threads import proxy_queue

reload(sys)
sys.setdefaultencoding('utf-8')
lock = threading.Lock()


class proxies(Base):
    __tablename__ = 'crawler_proxy_ip'

    proxy_id = Column(String(64), primary_key=True)
    ip = Column(String(128))
    source = Column(String(128))
    response_time = Column(Float)
    target_url = Column(String(128))
    insert_time = Column(Date)
    life_time = ''  # 有效期

    def __init__(self, ID):
        self.proxy_id = ID


def get_proxy_count():
    session = DBSession()
    count = session.query(func.count(proxies.proxy_id)).scalar()
    session.close()
    return count


def get_proxy():
    lock.acquire()
    while True:
        if get_proxy_count() > 0:
            break
        print 'has no proxy now!just sleep for a while'

    session = DBSession()
    proxy_list = session.query(proxies).all()
    proxy_ip = random.choice(proxy_list).ip
    proxy = {
        'http': 'http://' + proxy_ip,
        'https': 'https://' + proxy_ip
    }
    session.close()
    lock.release()
    return proxy


def reverse_proxy2ip(proxies):
    proxy_ip = proxies.ip
    proxy = {
        'http': 'http://' + proxy_ip,
        'https': 'https://' + proxy_ip
    }
    return proxy


def init_proxy_queue():
    session = DBSession()
    proxy_list = session.query(proxies).all()
    for p in proxy_list:
        proxy_ip = p.ip
        proxy = {
            'http': 'http://' + proxy_ip,
            'https': 'https://' + proxy_ip
        }
        try:
            proxy_queue.put(proxy)
        except Exception,e:
            print e.message


def save_proxy(proxy_save):
    session = DBSession()
    try:
        session.add(proxy_save)
        session.commit()
        proxy_ip = reverse_proxy2ip(proxy_save)
        proxy_queue.put(proxy_ip)
    except sqlalchemy.exc.IntegrityError, e:
        print e.message
    finally:
        session.close()


def del_proxy(proxies_del):
    lock.acquire()
    delete_ip = proxies_del['http'].replace('http://', '')
    session = DBSession()
    try:
        deleted_proxy = session.query(proxies).filter(proxies.ip == delete_ip).one()
        session.delete(deleted_proxy)
        session.commit()
        print "deleted"
    except Exception, e:
        print e.message
    finally:
        session.close()
    lock.release()
